local ffi = require('ffi');

---------------------------------------------------------------------------
--设置 require目录
function GetScriptPath()
    local info = debug.getinfo(GetScriptPath);
    return info.source:sub(2);
end

local function GetDir(v)
    local ps,pe,str = v:find('(.+[^/\\])[/\\][.%w%d%l_]+$');
    if(str==nil)then str = './' end;
    return str;
end

function GetScriptDir()
    return GetDir(GetScriptPath()) .. '/';
end

local dir = GetDir(GetScriptPath());

package.path = package.path .. ';' .. dir .. '/?.lua;'
--------------------------------------------------------------------------

local function LoadDef(fn)
    local file = io.open(GetScriptDir()..fn,"rb");
    local code = file:read('*a');
    ffi.cdef(code);
end


ffi.cdef[[
const char* SDL_GetPlatform();
]]
local SDL2 = ffi.load('SDL2');
local p = ffi.string(SDL2.SDL_GetPlatform());

if(p=='Windows')then
    WIN32 = true;
    if(not ANDROID)then
        LoadDef('lSDL2.h');
        LoadDef('lSDL2.c');
    end
elseif(p=='Android')then
    ANDROID = true;
    SDL2 = ffi.load('SDL2');
end

function GetAppDataPath()
    if(WIN32)then
        return "./"
    end
    if(ANDROID)then
        return ffi.string(SDL2.SDL_AndroidGetInternalStoragePath())..'/';
    end
end

if(ANDROID)then
    local loadlist = {};
    --有关安卓系统的加载库
    function loadso(sym)

        name = sym .. '.so';
        if(loadlist[name])then return loadlist[name]; end;
        --local sofile = io.open("/storage/emulated/0/ROM/"..name,"rb");
        local path = GetScriptDir() .. name;
        local sofile = io.open(path,"rb");
        local nsofile = io.open(GetAppDataPath() .. name,"rb");

        if(sofile==nil)then
            return ffi.load(sym);
        end
        

        --比较两者的数据
        local data = sofile:read('*a');
        assert(data,".so库加载失败"..path);

        local data2;
        if(nsofile)then
            data2 = nsofile:read('*a');
            nsofile:close();
        end

        if(data~=data2 or data)then 
            --不是相同的数据
            local write = io.open(GetAppDataPath() .. name,"wb+");
            write:write(data);
            write:flush();
            write:close();
        else
            --相同的数据
        end
        sofile:close();
        loadlist[name] = ffi.load(GetAppDataPath()..name);
        return loadlist[name]; 
    end

    --处理记录
    local log = io.open(GetScriptDir()..'out.log',"w+");
    if(log==nil)then log = io.stderr;  end;
    function print(...)
        local s = "";
        local a = {tostring(debug.getinfo(2).name),...};
        for k,v in ipairs(a)do
            s = s .. tostring(v) .. '\t';
        end
        log:write(s,"\n");
        log:flush();
    end

    function printf(fmt,...)
        local s = string.format(fmt,...);
        print(s);
    end

else
    --WIN32
    function loadso(name)
        return ffi.load(name);
    end
end

--获得文件目录和名字
function GetFilePathInfo(fn)
    local pos,npos,path,file = fn:find('(.-)([^/^\\]+)$');
    local pos,npos,nfile,ext = file:find('(.-)%.([^%.]+)$');
    if(nfile)then file = nfile end;
    return path,file,ext;
end

--print(GetFilePathInfo('aa'));

